<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 添加路由链接 -->
        <router-link to="/user/1">User</router-link>
        <router-link to="/user/2">User</router-link>
        <router-link :to="{name:'user',params:{id:3}}">User</router-link>
        <router-link to="/register">Register</router-link>
        <!-- 路由占位符 -->
        <router-view></router-view>
    </div>
    <script src="lib/vue_2.5.22.js"></script>
    <script src="lib/vue-router_3.0.2.js"></script>
    <script>
        // 定义路由组件
        const User = {
            // template: '<h1>User 组件 -- 用户id为: {{$route.params.id}}</h1>'
            props: ['id', 'uname', 'age'],
            // template: '<h1>User 组件 -- 用户id为: {{id}}</h1>'
            // template: `<h1>User 组件 -- 用户信息: {{id+'--'+uname+'--'+age}}</h1>`
            template: `<div>
                <h1>User 组件 -- 用户信息: {{id+'--'+uname+'--'+age}}</h1>
                <button @click="goRegister">跳转</button>
            </div>`,
            methods: {
                goRegister() {
                    this.$router.push('/register')
                }
            },
        }
        const Register = {
            template: `<div>
                <h1>Register 组件</h1>
                <button @click="goBack">后退</button>
            </div>`,
            methods: {
                goBack() {
                    this.$router.go(-1)
                }
            },
        }
        // 创建路由实例
        const router = new VueRouter({
            // 配置路由规则
            routes: [
                //路由重定向    
                {
                    path: '/',
                    redirect: '/user'
                }, {
                    path: '/user/:id',
                    //命名路由
                    name: 'user',
                    component: User,
                    // props: true
                    props: route => ({
                        uname: 'lisi',
                        age: 12,
                        id: route.params.id
                    })
                }, {
                    path: '/Register',
                    component: Register
                }
            ]
        })
        const vm = new Vue({
            el: '#app',
            // 挂载
            router,
            data: {

            }
        })
    </script>
</body>

</html>